package com.jr.util;

import org.apache.catalina.User;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Properties;

/**
 * 数据库工具类
 */
public class C3P0Test{

    private static  String url ;
    private static  String username ;
    private static  String password ;
    private static  int initSize;
    private static  int maxSize ;

    private static LinkedList<Connection> pool;

    static {

        try {
            Properties pro=new Properties();
            InputStream resourceAsStream = DBHelper.class.getClassLoader().getResourceAsStream("jdbc.properties");
            pro.load(resourceAsStream);
            url= pro.getProperty("mysql.url");
            username= pro.getProperty("mysql.username");
            password= pro.getProperty("mysql.password");
            initSize= Integer.parseInt(pro.getProperty("mysql.initSize"));
            maxSize=  Integer.parseInt(pro.getProperty("mysql.maxSize"));

            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException | IOException e) {
            e.printStackTrace();
        }
        pool = new LinkedList<>();
        for (int i = 1; i <= initSize; i++) {
            pool.add(initConnection());
        }
    }

    private static Connection initConnection() {
        try {
            return DriverManager.getConnection(url, username, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /*从连接池中获得连接对象*/
    public static  Connection getCon() {
        if (pool.size() > 0) {
            return pool.removeFirst();
        } else {
            return initConnection();
        }
    }

    public static  void main(String[] args){
        System.out.println(getCon());
    }

    /*归还connection对象*/
    private void  returnConnection(Connection con){
        try {
            if (con!=null && con.isClosed()){
                if(pool.size()<maxSize){
                    pool.addLast(con);
                }else{
                    con.close();
                }

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }




    //7
    public void closeAll(Connection con, PreparedStatement ps, ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
            System.out.println(pool.size()+"。。。。。。。。。。。。连接池对象个数");
            returnConnection(con);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    //增删改的通用方法
    public int dtUpdate(String sql, Object... obj) { //3
        Connection con = getCon(); //1，2
        PreparedStatement ps = null;
        int count = 0;
        try {
            ps = con.prepareStatement(sql);//4
            for (int i = 0; i < obj.length; i++) {
                ps.setObject(i + 1, obj[i]);
            }
            count = ps.executeUpdate(); //5  6返回，谁调用谁处理
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(con, ps, null); //7
        }
        return count;
    }

    //通用的查询方法
    public ArrayList dtQuery(String sql, Class cla, Object... obj) {
        ArrayList list = new ArrayList();
        Connection con = getCon();
        System.out.println(pool.size()+"。。。。。。。。。。。。连接池对象个数");
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = con.prepareStatement(sql);
            for (int i = 0; i < obj.length; i++) {
                ps.setObject(i + 1, obj[i]);
            }
            rs = ps.executeQuery();
            while (rs.next()) {
                Object o = cla.newInstance();
                Field[] fields = cla.getDeclaredFields();
                for (Field f : fields) {
                    f.setAccessible(true);
                    f.set(o, rs.getObject(f.getName()));
                }
                list.add(o);
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(con, ps, rs);
        }
        return list;
    }

}
